package nullby.omnia.activity.theme;

import nullby.common.utils.CustomLog;
import nullby.omnia.R;
import nullby.omnia.activity.card.ActivityAddCard;
import nullby.omnia.adapter.AdapterThemes;
import nullby.omnia.db.DatabaseHelper;
import nullby.omnia.model.Theme;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class ActivityTheme extends SherlockFragmentActivity {

	private static final String TAG = ActivityTheme.class.getSimpleName();

	private ListView lvWords;

	private String nameTheme;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		CustomLog.logI(TAG, "onCreateOptionsMenu");
		menu.add("COUNT")
				.setTitle(
						DatabaseHelper.get().getCountCard(
								getIntent().getStringExtra("idCard")))
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		menu.add("ADD")
				.setIcon(R.drawable.content_new)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_WITH_TEXT);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		CustomLog.logI(TAG, "onOptionsItemSelected");
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		if ("ADD".equals(item.toString())) {
			startActivity(new Intent(this, ActivityAddCard.class));
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		CustomLog.logI(TAG, "onCreate");
		super.onCreate(arg0);
		setContentView(R.layout.activity_single_theme);
		Theme themeInfo = DatabaseHelper.get().getThemeById(
				getIntent().getStringExtra("idCard"));
		nameTheme = themeInfo.getName();

		AdapterThemes adapter = new AdapterThemes(this, DatabaseHelper.get()
				.getAllWordsByTheme(getIntent().getStringExtra("idCard")));
		// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
		// android.R.layout.simple_list_item_1, DatabaseHelper.get()
		// .getAllWordsByTheme(
		// getIntent().getStringExtra("idCard")), null,
		// null);
		lvWords = (ListView) findViewById(R.id.lvWords);
		lvWords.setAdapter(adapter);
		initView();
	}

	protected void initView() {
		CustomLog.logI(TAG, "initView");

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle(nameTheme);
	}
}
